package com.sky.controller.admin;

import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetMealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/setmeal")
@Slf4j
@Api(tags = "套餐管理接口")
public class SetMealController{
    @Autowired
    SetMealService setMealService;
    @ApiOperation("新增套餐")
    @PostMapping
    @CacheEvict(cacheNames = "setmealCache",key = "#setmealDTO.categoryId")
    public Result save(@RequestBody SetmealDTO setmealDTO){
        setMealService.save(setmealDTO);
        return Result.success();
    }

    @ApiOperation("分页查询接口")
    @GetMapping("/page")
    public Result page(SetmealPageQueryDTO setmealPageQueryDTO)
    {
        PageResult page=setMealService.page(setmealPageQueryDTO);
        return Result.success(page);
    }

    /**
     *
     * @param setmealDTO
     * @return
     */
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    @ApiOperation("修改套餐")
    @PutMapping
    public Result update(@RequestBody SetmealDTO setmealDTO)
    {   log.info(String.valueOf(setmealDTO));
        setMealService.update(setmealDTO);
        return Result.success();
    }
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    /**
     *
     * @return
     */
    @ApiOperation("起售停售")
    @PostMapping("/status/{status}")
    public Result startOrStop(@PathVariable Integer status,Long id)
    {
        setMealService.startOrStop(status,id);
        return Result.success();
    }

    /**
     *
     * @param id
     * @return
     */
    @ApiOperation("根据Id查询套餐")
    @GetMapping("/{id}")
    public Result getById(@PathVariable Long id)
    {
        SetmealVO setmealVO=setMealService.getById(id);
        return Result.success(setmealVO);
    }
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    @ApiOperation("批量删除套餐")
    @DeleteMapping
    public Result deleteByIds(@RequestParam List<Long> ids)
    {   setMealService.deleteByIds(ids);
        return Result.success();
    }

}
